package goemail

import (
	"github.com/gogf/gf/frame/g"
	"gopkg.in/gomail.v2"
	"strconv"
)

var Email = new(EmailParam)

type EmailParam struct {}

const (
	Subject string = "blizzard邮箱验证码"
	RegBody string = `注册`
	QBody string = `<head>
		<base target="_blank" />
		<style type="text/css">::-webkit-scrollbar{ display: none; }</style>
		<style id="cloudAttachStyle" type="text/css">#divNeteaseBigAttach, #divNeteaseBigAttach_bak{display:none;}</style>
		<style id="blockquoteStyle" type="text/css">blockquote{display:none;}</style>
		<style type="text/css">
			body{font-size:14px;font-family:arial,verdana,sans-serif;line-height:1.666;padding:0;margin:0;overflow:auto;white-space:normal;word-wrap:break-word;min-height:100px}
			td, input, button, select, body{font-family:Helvetica, 'Microsoft Yahei', verdana}
			pre {white-space:pre-wrap;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;word-wrap:break-word;width:95%}
			th,td{font-family:arial,verdana,sans-serif;line-height:1.666}
			img{ border:0}
			header,footer,section,aside,article,nav,hgroup,figure,figcaption{display:block}
			blockquote{margin-right:0px}
		</style>
	</head>
	<body tabindex="0" role="listitem">
	<table width="700" border="0" align="center" cellspacing="0" style="width:700px;">
		<tbody>
		<tr>
			<td>
				<div style="width:700px;margin:0 auto;border-bottom:1px solid #ccc;margin-bottom:30px;">
					<table border="0" cellpadding="0" cellspacing="0" width="700" height="39" style="font:12px Tahoma, Arial, 宋体;">
						<tbody><tr><td width="210"></td></tr></tbody>
					</table>
				</div>
				<div style="width:680px;padding:0 10px;margin:0 auto;">
					<div style="line-height:1.5;font-size:14px;margin-bottom:25px;color:#4d4d4d;">
						<strong style="display:block;margin-bottom:15px;">尊敬的用户：<span style="color:#f60;font-size: 16px;"></span>您好！</strong>
						<strong style="display:block;margin-bottom:15px;">`
	HBody string = `您正在进行<span style="color: red">`
	PBody string = `</span>操作，请在验证码输入框中输入：<span style="color:#f60;font-size: 24px">`
	LBody string = `</span>，以完成操作。
						</strong>
					</div>
					<div style="margin-bottom:30px;">
						<small style="display:block;margin-bottom:20px;font-size:12px;">
							<p style="color:#747474;">
								注意：此操作可能会修改您的密码、登录邮箱或绑定手机。如非本人操作，请及时登录并修改密码以保证帐户安全
								<br>
							</p>
						</small>
					</div>
				</div>
				<div style="width:700px;margin:0 auto;">
					<div style="padding:10px 10px 0;border-top:1px solid #ccc;color:#747474;margin-bottom:20px;line-height:1.3em;font-size:12px;">
						<p>此为系统邮件，请勿回复<br>
							请保管好您的邮箱，避免账号被他人盗用
						</p>
						<p>blizzard网络</p>
					</div>
				</div>
			</td>
		</tr>
		</tbody>
	</table>
	</body>`
)

//发送邮件的逻辑函数
func (s *EmailParam) SendMail(mailTo []string, subject string, body string) error {
	//定义邮箱服务器连接信息，如果是网易邮箱 pass填密码，qq邮箱填授权码

	//mailConn := map[string]string{
	//  "user": "xxx@163.com",
	//  "pass": "your password",
	//  "host": "smtp.163.com",
	//  "port": "465",
	//}

	mailConn := map[string]string{
		"user": g.Cfg().GetString("email.user"),
		"pass": g.Cfg().GetString("email.pass"),
		"host": g.Cfg().GetString("email.host"),
		"port": g.Cfg().GetString("email.port"),
	}

	port, _ := strconv.Atoi(mailConn["port"]) //转换端口类型为int

	m := gomail.NewMessage()

	m.SetHeader("From",  m.FormatAddress(mailConn["user"], "Blizzard")) //这种方式可以添加别名，即“XX官方”
	//说明：如果是用网易邮箱账号发送，以下方法别名可以是中文，如果是qq企业邮箱，以下方法用中文别名，会报错，需要用上面此方法转码
	//m.SetHeader("From", "FB Sample"+"<"+mailConn["user"]+">") //这种方式可以添加别名，即“FB Sample”， 也可以直接用<code>m.SetHeader("From",mailConn["user"])</code> 读者可以自行实验下效果
	//m.SetHeader("From", mailConn["user"])
	m.SetHeader("To", mailTo...)    //发送给多个用户
	m.SetHeader("Subject", subject) //设置邮件主题
	m.SetBody("text/html", body)    //设置邮件正文

	d := gomail.NewDialer(mailConn["host"], port, mailConn["user"], mailConn["pass"])

	err := d.DialAndSend(m)
	return err
}

func (s *EmailParam) GetEmailHtml(name, code string) string {
	return QBody + HBody + name + PBody + code + LBody
}